package utils.path.support;

import utils.path.ConfigPath;
import utils.path.PathResolver;
import utils.test.Assert;

/**
 * 路径解决
 */
public class PathResolverImpl implements PathResolver {

	public String[] resolve(String pathPattern) {
		ConfigPath cfgPath = resolveConfigPath(pathPattern);
		return cfgPath.getLocation();
	}

	public ConfigPath resolveConfigPath(String pathPattern) {
		Assert.isNotNull(pathPattern, "Path pattern is not allow null.");
		ConfigPath cfgPath = null;
		if (pathPattern.trim().startsWith(CLASS_PATH_PREX)) {
			pathPattern = pathPattern.substring(CLASS_PATH_PREX.length());
			cfgPath = new ClassConfigPath(pathPattern);
		}else if(pathPattern.trim().startsWith(FILE_SYSTEM_PREX)){
			pathPattern = pathPattern.substring(FILE_SYSTEM_PREX.length());
			cfgPath = new FileSystemConfigPath(pathPattern);
		}else if(pathPattern.trim().startsWith(WORK_DIR_REGX)){
			pathPattern = pathPattern.substring(WORK_DIR_REGX.length());
			cfgPath = new WorkDirConfigPath(pathPattern);
		}else{
			throw new IllegalArgumentException("Path pattern is not identify.");
		}
		return cfgPath;
	}

}
